<script setup lang="ts">
import { useMenuStore } from '@/stores/menu';
import { useUserStore } from '@/stores/user';
import { useUserDetailStore } from '@/stores/userDetail';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useRouter } from 'vue-router';

const userStore = useUserStore()
const userDetailStore = useUserDetailStore()
const menuStore = useMenuStore()
const router = useRouter()

// 退出登录
const backLogin = async () => {
  // 确认框
  try {
    await ElMessageBox.confirm("确定退出登录吗？", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    
    userStore.removeToken() // 删除token
    userDetailStore.clearUserDetail() // 清空用户信息
    menuStore.clearMenuList() // 重置路由列表
    router.push('/login') // 跳转登录页
    ElMessage.success('退出成功')

  } catch (error) {}
}

</script>

<template>
  <div class="nav-bar">
    <!-- breadcrumb -->
    <div></div>
    <!-- dropdown -->
    <el-dropdown>
      <div class="user-wrapper" style="font-weight: 800; cursor: pointer;">{{ userDetailStore.userDetail.name }}</div>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item @click="$router.push('/')">首页</el-dropdown-item>
          <el-dropdown-item>项目地址</el-dropdown-item>
          <el-dropdown-item>修改密码</el-dropdown-item>
          <el-dropdown-item @click="backLogin">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<style lang="scss" scoped>
.nav-bar {
  height: 60px;
  overflow: hidden;
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;

  .user-wrapper {
    outline: none;
    cursor: default;
    color: var(--el-color-black);
  }
}
</style>
